<?php

namespace App\Http\Controllers\Manage;

use App\Http\Model\Manage\AdminModel;
use App\Http\Model\Manage\AdminRoleModel;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class LoginController extends BasicController
{
    /**
     * 加载登录页面
     *
     * @return View
     */
    public function loginPageView(): View
    {
        return view($this->viewPath."login");
    }

    /**
     * 登录
     * <p>
     *     1. 获取登录参数
     *     2. 验证码验证是否正确
     *     3. 验证账号登录次数 同一个账号不能超过5次
     *     4. 验证账号密码 是否存在 是否被删除 是否被禁用
     *     5. 登录成功 保存登录信息 更新登录信息
     *     6. 获取当前账号拥有的角色
     * </p>
     *
     * @param Request $request
     * @return array|string
     */
    public function loginInAction(Request $request): array|string
    {
        $data = $request->all();
        $captchaCheck = $this->checkCaptcha($data["captcha"]);
        if (!$captchaCheck) {
            return errorReturn("验证码错误");
        }
        // 验证账号多次登录
        $manageLoginNum = $request->session()->get($data["username"], 0);
        if ($manageLoginNum >= 5) {
            return errorReturn("单个账号多次登录错误警告");
        }
        $request->session()->put($data["username"], $manageLoginNum + 1);
        $password = md5(md5($data["password"]."123456789")."asdfghjkl");
        $safety = substr($password,3,8);
        $admin = AdminModel::where(dataToWhere([
            "lta001"=>$data["username"],
            "lta002"=>$password,
            "lta003"=>$safety,
            "deleted"=>0
        ]))->first();
        if (!$admin) {
            return errorReturn("用户不存在");
        }
        if ($admin->status != 1) {
            return errorReturn("用户被禁用");
        }
        (new AdminModel)->updateInfo([
            ["code", "=", $admin->code],
            ["version", "=", $admin->version]
        ]);
        $admin->version = $admin->version + 1;
        $request->session()->forget($data["username"]);
        $request->session()->put([
            "manage_login_user" => true,
            "manage_admin"=>$admin
        ]);
        // 使用，号分割的角色编码转换为数组
        $roleCode = explode(",", $admin->ltarCodes);
        $adminRole = AdminRoleModel::whereIn("code", $roleCode)->where(dataToWhere([
            "deleted"=>0,
            "status"=>1
        ]))->get();
        $this->addLog("管理员登录成功", "登录成功", $request, __CLASS__,  __DIR__.",".__FILE__, __METHOD__.",".__FUNCTION__, __LINE__);
        return successReturn("登录成功", ["adminRole"=>$adminRole] );
    }

    /**
     * 退出登录
     * <p>
     *     1. 删除 session
     * </p>
     * @param Request $request
     * @return array|string
     */
    public function loginOutAction(Request $request): array|string
    {
        $request->session()->flush();
        return successReturn("退出登录");
    }

    /**
     * 切换角色
     * <p>
     *     1. 验证当前账号是否拥有该角色
     *     2. 验证角色是否被禁用 是否被删除
     *     3. 角色切换成功 保存角色信息到 session
     * </p>
     * @param Request $request
     * @return array|string
     */
    public function adminSelectRole(Request $request): array|string
    {
        $data = $request->all();
        $admin = $request->session()->get("manage_admin");
        $roleCodes = explode(",", $admin->ltarCodes);
        if (in_array($data["code"], $roleCodes)) {
            $role = AdminRoleModel::where(dataToWhere([
                "code"=>$data["code"],
                "deleted"=>0,
                "status"=>1
            ]))->first();
            if (!$role) {
                return errorReturn("切换失败, 角色不存在或已被禁用");
            }
            $request->session()->put([
                "manage_role"=>$role
            ]);
            return successReturn("切换成功");
        }
        return errorReturn("切换失败, 角色不存在");
    }

    /**
     * 获取当前账号角色列表
     * <p>
     *     1. 获取当前账号信息
     *     2. 获取当前账号拥有的角色
     * </p>
     * @param Request $request
     * @return array|string
     */
    public function adminRoleList(Request $request): array|string
    {
        $admin = $request->session()->get("manage_admin");
        // 使用，号分割的角色编码转换为数组
        $roleCode = explode(",", $admin->ltarCodes);
        $adminRole = AdminRoleModel::whereIn("code", $roleCode)->where(dataToWhere([
            "deleted"=>0,
            "status"=>1
        ]))->get();
        return successReturn("加载成功", ["adminRole"=>$adminRole] );
    }
}
